﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using SnakeGame.Views;
using SnakeGame.Models;

namespace SnakeGame.ViewModels
{
    class CharacterPageViewModel : NotifyPropertyChanged 
    {
        public ICommand NextCommand { get; set; }
        public ICommand SelectPlayerCommand { get; set; }
        public ICommand SelectCharacterCommand { get; set; }
        public int NumberOfPlayer { get; set; }
        private int currentPlayer;
        private MainFrame _mainFrame;

        private ObservableCollection<Player> _playerList;
        public ObservableCollection<Player> PlayerList { get { return _playerList; } }
        private ObservableCollection<CharacterModel> _characterList;
        public ObservableCollection<CharacterModel> CharacterList { get { return _characterList; } }

        public class CharacterModel
        {
            public CharacterModel(string pic)
            {
                this.Picture = pic;
            }
            public string Picture { get; set; }
        };

        public CharacterPageViewModel()
        {
            SelectCharacterCommand = new RelayCommand(OnSelectCharacterCommand);
            SelectPlayerCommand = new RelayCommand(OnSelectPlayerCommand);
            NextCommand = new RelayCommand(OnNextCommand);

            _characterList = new ObservableCollection<CharacterModel>();
            _characterList.Add(new CharacterModel(@"pack://application:,,,/Resources/1.png"));
            _characterList.Add(new CharacterModel(@"pack://application:,,,/Resources/2.png"));
            _characterList.Add(new CharacterModel(@"pack://application:,,,/Resources/3.png"));
            _characterList.Add(new CharacterModel(@"pack://application:,,,/Resources/4.png"));
            _characterList.Add(new CharacterModel(@"pack://application:,,,/Resources/5.png"));
            _characterList.Add(new CharacterModel(@"pack://application:,,,/Resources/6.png"));
        }

        public void Initialize(MainFrame mainFrame)
        {
            _mainFrame = mainFrame;
            _playerList = _mainFrame.Players;
            OnPropertyChanged("PlayerList");
        }

        private void OnSelectCharacterCommand(object obj)
        {
            var item = obj as CharacterModel;
            _playerList[currentPlayer].CharacterPicture = item.Picture;
        }

        private void OnSelectPlayerCommand(object obj)
        {
            if (obj == null)
                return;

            var item = obj as Player;
            currentPlayer = item.Index;
            
        }

        private void OnNextCommand(object obj)
        {
            GamePage g = new GamePage();
            _mainFrame.Navigate(g);
            g.Initialize(_mainFrame);
        }   
    }
}
